package com.mb.mp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mb.common.utils.Query;
import com.mb.common.utils.StringUtils;
import com.mb.mp.constants.AdminConstants;
import com.mb.mp.dao.IBrandAuxiliaryClassDAO;
import com.mb.mp.dao.IHxhBrandDAO;
import com.mb.mp.domain.HxhBrandAuxiliaryClassDO;
import com.mb.mp.domain.HxhBrandDO;
import com.mb.mp.service.IBrandService;
import com.mb.system.dao.UserDao;
import com.mb.system.domain.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wangyuan
 * @program mp-admin
 * @date 2018/9/7 11:32
 **/
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private IHxhBrandDAO brandDAO;
    @Autowired
    private IBrandAuxiliaryClassDAO auxiliaryClassDAO;
    @Autowired
    private UserDao userDao;
    /** 
     * @description: 列表
     * @author: wangyuan
     * @date: 2018-09-07 11:56:13
     */
    @Override
    public List<HxhBrandDO> list(Query query) {
        String brandName =(String)query.get("brandName");
        if(StringUtils.isNotBlank(brandName)){
            query.setOffset(0);
        }
        List<HxhBrandDO> brandList = brandDAO.list(query);
        if(brandList.size() >0){
            for(HxhBrandDO brand :brandList){
                List<String> auxiliaryList = auxiliaryClassDAO.selectByBrandId(brand.getBrandId());
               brand.setCheckList(auxiliaryList);
                if (brand.getCreator() != null){
                    UserDO userDO = userDao.get(brand.getCreator());
                    if (userDO != null){
                        brand.setCreateName(userDO.getName());
                    }
                }
            }

        }
        return brandList;
    }

    @Override
    public Integer count(Query query) {
        return brandDAO.count(query);
    }

    /** 
     * @description: 品牌添加
     * @author: wangyuan
     * @date: 2018-09-07 12:14:25
     */
    @Override
    @Transactional
    public JSONObject insertBrand(HxhBrandDO hxhBrandDO) {
        JSONObject resData = new JSONObject();
        // 校验该品牌名称是否重复
        String bName = hxhBrandDO.getBrandName();
         String brandName = bName.replaceAll(" ", "");
        int num = brandDAO.selectByBrandName(brandName);
        if (num > 0){
            resData.put("code", AdminConstants.Base.FAILED.getCode());
            resData.put("msg","此品牌名称已存在");
            return resData;
        }
        // 查看品牌热门数量是否超过12个
        int hots = brandDAO.selectByHots();
        if(hxhBrandDO.getIsHot().intValue() == 1){
            if(hots >11){
                resData.put("code", AdminConstants.Base.FAILED.getCode());
                resData.put("msg","热门品牌不能超过12个");
                return resData;
            }


        }
        int count = brandDAO.insertSelective(hxhBrandDO);
        if(count >0){
            // 品牌ID关联七大类
            Long brandId =  hxhBrandDO.getBrandId();
            List<String> auxiliaryList = hxhBrandDO.getCheckList();
            HxhBrandAuxiliaryClassDO auxiliaryClassDO = new HxhBrandAuxiliaryClassDO();
            auxiliaryClassDO.setBrandId(brandId);
            if(auxiliaryList.size() >0){
                for (String auxiliary :auxiliaryList){
                    auxiliaryClassDO.setUxiliaryClass(auxiliary);
                    auxiliaryClassDAO.insertSelective(auxiliaryClassDO);
                }
            }
            resData.put("code", AdminConstants.Base.SUCCESS.getCode());
            resData.put("msg",AdminConstants.Base.SUCCESS.getMsg());
        }
        return resData;
    }

    /** 
     * @description: 品牌更新
     * @author: wangyuan
     * @date: 2018-09-07 12:17:06
     */
    @Override
    @Transactional
    public JSONObject update(HxhBrandDO hxhBrandDO) {
        JSONObject resData = new JSONObject();
        // 校验该品牌名称是否重复
        String bName = hxhBrandDO.getBrandName();
        String brandName = bName.replaceAll(" ", "");
        int num = brandDAO.selectByBrandName(brandName);
        //排除当前名字
        HxhBrandDO brand = brandDAO.selectByPrimaryKey(hxhBrandDO.getBrandId());
        if(!brandName.equals(brand.getBrandName())){
            if (num > 0){
                resData.put("code", AdminConstants.Base.FAILED.getCode());
                resData.put("msg","此品牌名称已存在");
                return resData;
            }
        }
        //排除当前是热门的
        if(brand.getIsHot().intValue() == 0){
            // 查看品牌热门数量是否超过12个
            int hots = brandDAO.selectByHots();
            if(hxhBrandDO.getIsHot().intValue() == 1){
                if(hots >11){
                    resData.put("code", AdminConstants.Base.FAILED.getCode());
                    resData.put("msg","热门品牌不能超过12个");
                    return resData;
                }

            }
        }

        //先把关联七大类删除，再插入
        Long brandId = hxhBrandDO.getBrandId();
        auxiliaryClassDAO.deleteByBrandId(brandId);
        int count = brandDAO.updateByPrimaryKeySelective(hxhBrandDO);
        if(count >0){
            // 插入七大类
            List<String> auxiliaryList = hxhBrandDO.getCheckList();
            HxhBrandAuxiliaryClassDO auxiliaryClassDO = new HxhBrandAuxiliaryClassDO();
            auxiliaryClassDO.setBrandId(brandId);
            if(auxiliaryList.size() >0){
                for (String auxiliary :auxiliaryList){
                    auxiliaryClassDO.setUxiliaryClass(auxiliary);
                    auxiliaryClassDAO.insertSelective(auxiliaryClassDO);
                }
            }
            resData.put("code", AdminConstants.Base.SUCCESS.getCode());
            resData.put("msg",AdminConstants.Base.SUCCESS.getMsg());
        }
        return resData;
    }

    @Override
    public Integer delete(HxhBrandDO hxhBrandDO) {
        return brandDAO.updateByPrimaryKeySelective(hxhBrandDO);
    }
}
